package com.ciwong.epaper.modules.me.ui;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.cordva.BaseHtmlActicity;
import com.ciwong.epaper.modules.cordva.SubmitEvent;
import com.ciwong.epaper.modules.me.bean.JumpServiceParamBean;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.epaper.util.RequestCode;
import com.ciwong.eventbus.EventBus;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.ciwong.mobilelib.i.ViewOnClickListener;
import com.google.gson.Gson;

/**
 * Created by yyf on 2017/4/17.
 */

public class ResourceCenterH5Activity extends BaseHtmlActicity {
    /**
     * 我的服务列表
     */
    public static final int REQUEST_CODE_TO_MY_SERVICES = 0x0000016;
    private static final String TAG = "ResourceCenterH5Activit";
    private String title;
    Gson gson = new Gson();

    @Override
    protected void preCreate() {
        super.preCreate();
        Intent intent = getIntent();
        title = intent.getStringExtra(IntentFlag.INTENT_FLAG_TITLE);
    }

    @Override
    protected void init() {
        super.init();
        setValideSource(false);
//        if (title !=null)
//            setTitleText(title);
        setRightBtnText(R.string.my_orders);
        setRightBtnListener(new ViewOnClickListener() {
            @Override
            public void avertRepeatOnClick(View v) {
                //我的服务列表
                MeJumpManager.jumpToMyServices(R.string.go_back, ResourceCenterH5Activity.this, REQUEST_CODE_TO_MY_SERVICES);
            }
        });
        BaseRequest.VerifyInfo verifyInfo = HttpRequest.getVerifyInfo();
        if (verifyInfo != null) {
            String clientId = verifyInfo.getClientId();
            String userId = EApplication.getInstance().getUserInfoBase().getUserId() + "";
            String brandId = EApplication.BRAND_ID + "";
            String startURL = EpaperConstant.URL_H5_RECOURSE_CENTER + "clientId=" + clientId + "&userId=" + userId + "&brandId=" + brandId;
            setStartURL(startURL);
            loadUrl(startURL.toString());
        } else {
            showToastError("无法获取用户信息，请重新登录后重试");
            finish();
        }
    }


    @Override
    protected void loadData() {
    }

    @Override
    protected void initEvent() {
        super.initEvent();
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }


    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void finish() {
        if (webView.canGoBack()) {
            webView.goBack();
            return;
        }
        super.finish();
    }


    public void onEventMainThread(SubmitEvent submitEvent) {
        Log.d(TAG, "onEventMainThread+" + submitEvent.getId() + "--Json:" + submitEvent.getJson());
        if (submitEvent.getId().equals("JumpServiceDetails")) {//
            JumpServiceParamBean jumpServiceParaBean = gson.fromJson(submitEvent.getJson(), JumpServiceParamBean.class);
            int serviceId = jumpServiceParaBean.getServiceId();
            String urlH5 = jumpServiceParaBean.getUrl();
            if (urlH5 == null || urlH5.equals(""))//当地址不存在时就跳到服务页面
                jumpToBookOrServerDetails(serviceId);
            else
                jumpToInnerResouce(urlH5);
        }
    }

    /**
     * 跳入h5内部资源
     *
     * @param urlH5
     */
    private void jumpToInnerResouce(String urlH5) {
        if (!urlH5.contains("http")) {//解决连接中不包含http 导致闪退的bug
            urlH5 = "http://" + urlH5;
        }
        MeJumpManager.jumpToResourseCenterInnerH5Activity(R.string.go_back, this, urlH5);

    }

    /**
     * 调到服务详情（购买页）
     */
    private void jumpToBookOrServerDetails(int serviceId) {
        String checkValidMsg = null;
        MeJumpManager.jumpBuyService(this, R.string.go_back, serviceId, -1, RequestCode.MainActivityResquestCode.REQUEST_CODE_TO_PAY, checkValidMsg);
    }
}
